﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myTools
{
    public partial class ucMd5 : UserControl
    {
        public ucMd5()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
               tbxNew.Text = getMd5(tbxOld.Text);
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        public static string MD5Stream(Stream stream)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(stream);
            byte[] b = md5.Hash;
            md5.Clear();
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < b.Length; i++)
            {
                sb.Append(b[i].ToString("X2"));
            }
            return sb.ToString();
        }


        /// <summary>
        /// 对文件进行MD5加密
        /// </summary>
        public static string MD5Stream(string filePath)
        {
            using (FileStream stream = File.Open(filePath, FileMode.Open))
            {
                return MD5Stream(stream);
            }
        }


        /// <summary>
         /// MD5加密
         /// </summary>
         /// <param name="strSource">需要加密的明文</param>
         /// <returns>返回32位加密结果</returns>
        public static string Get_MD5(string strSource, string sEncode)
        {
            //new 
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            //获取密文字节数组
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.GetEncoding(sEncode).GetBytes(strSource));
            //转换成字符串，并取9到25位 
            //string strResult = BitConverter.ToString(bytResult, 4, 8);  
            //转换成字符串，32位 
            string strResult = BitConverter.ToString(bytResult);
            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符，需要去除掉 
            strResult = strResult.Replace("-", "");
            return strResult.ToLower();
        }

        public string getMd5(string userPwd)
        {

            //获取userPwd的byte类型数组
            byte[] byteUserPwd = Encoding.UTF8.GetBytes(userPwd);

            //实例化MD5CryptoServiceProvider
            MD5CryptoServiceProvider myMd5 = new MD5CryptoServiceProvider();

            // byte类型数组的值转换为 byte类型的Md5值
            byte[] byteMd5UserPwd = myMd5.ComputeHash(byteUserPwd);

            //将byte类型的Md5值转换为字符串
            //string strMd5Pwd = Encoding.Default.GetString(byteMd5UserPwd).Trim();

            //返回Md5字符串
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < byteMd5UserPwd.Length; i++)
            {
                sb.Append(byteMd5UserPwd[i].ToString("X2"));
            }
            return sb.ToString();
        }

    }
}
